package com.fankai.lease.web.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fankai.lease.model.entity.*;
import com.fankai.lease.web.app.mapper.*;
import com.fankai.lease.web.app.service.LeaseAgreementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fankai.lease.web.app.vo.agreement.AgreementDetailVo;
import com.fankai.lease.web.app.vo.agreement.AgreementItemVo;
import com.fankai.lease.web.app.vo.graph.GraphVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Resource
    private LeaseAgreementMapper leaseAgreementMapper;

    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;

    @Resource
    private RoomInfoMapper roomInfoMapper;

    @Resource
    private PaymentTypeMapper paymentTypeMapper;

    @Resource
    private LeaseTermMapper leaseTermMapper;

    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Override
    public AgreementDetailVo selectDetailById(Long id) {
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        if (leaseAgreement==null){
            return  null;
        }
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement,agreementDetailVo);

        Long apartmentId = leaseAgreement.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(apartmentId);
        List<GraphVo> graphVos = graphInfoMapper.selectGraphVoListByApartId(apartmentId);

        Long roomId = leaseAgreement.getRoomId();
        RoomInfo roomInfo = roomInfoMapper.selectById(roomId);
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVoListByRoomId(roomId);


        Long paymentTypeId = leaseAgreement.getPaymentTypeId();
        PaymentType paymentType = paymentTypeMapper.selectById(paymentTypeId);

        Long leaseTermId = leaseAgreement.getLeaseTermId();
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseTermId);

        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        agreementDetailVo.setRoomGraphVoList(graphVoList);
        agreementDetailVo.setApartmentGraphVoList(graphVos);
        return agreementDetailVo;
    }

    @Override
    public List<AgreementItemVo> listItemByPhone(String phone) {
        return leaseAgreementMapper.listItemByPhone(phone);
    }
}




